package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.PopupClickListener;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.mapapi.utils.DistanceUtil;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.adapter.CityAreaAdapter;
import com.kuanter.kuanterauto.adapter.CityCircleAdapter;
import com.kuanter.kuanterauto.adapter.PopServiceAdapter;
import com.kuanter.kuanterauto.adapter.PopServiceTypeAdapter;
import com.kuanter.kuanterauto.adapter.PopSortAdapter;
import com.kuanter.kuanterauto.adapter.SitesAdapter;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.CityArea;
import com.kuanter.kuanterauto.entity.CityArea.Circle;
import com.kuanter.kuanterauto.entity.KuanterService;
import com.kuanter.kuanterauto.entity.KuanterService.Service;
import com.kuanter.kuanterauto.entity.SiteInfo;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.AnimationController;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.JsonParser;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
import com.kuanter.kuanterauto.view.PollListView;
import com.umeng.analytics.MobclickAgent;

public class ServiceTurnOver1 extends BaseActivity_NoBar implements OnClickListener {
	public String FETCHSIZE = "10";
	
	static boolean localCity = false;
	String cityName;
	public String cityCenter;
	
	String orderType = "0";	//排序
	String dis = "5";		//距离
	
	String otherCity_center = "";
	
	static boolean tap = false;   //是否改变了颜色
	
	private String otherCityDefault = "全城";
	private String center;
	
	ImageView dingwei;
	ImageView zoom_out;
	ImageView zoom_in;
	
	RelativeLayout map_modern;
	PollListView list_modern;
	ViewGroup mContainer;
	int over_status = 1;  // <0表示列表模式，大于0则地图模式
	
	
	//List模式
	RelativeLayout re_distanse_click;   //距离选择点击的layout
	RelativeLayout re_condiction_click;  //服务类型选择点击的layout
	RelativeLayout re_sort_click; 		//排序选择点击的layout
	
	PopupWindow mPopWindow_distance;		//距离选择后弹出的popwindow
	RadioButton radio1;			//可选择的距离
	RadioButton radio2;
	RadioButton radio3;
	RadioButton radio4;
	TextView chosed_distance;   //选择的距离的显示在页面
	ImageView distance_arrow;	//图片，上还是下
	
	PopupWindow mPopWindow_condition;   //服务选择后弹出的popwindow
	TextView chosed_service_type;		//选择的服务的显示
	ImageView condiction_arrow;			//图片，弹出pop后为上，pop消失后为下
	
	PopupWindow mPopWindow_sort;		//排序选择后弹出的popwindow
	TextView chosed_sort;			//选择的排序
	ImageView sort_arrow;			//图片，弹出pop后为上，pop消失后为下
	
	
	PollListView sites_list;	//网点列表listview
	
	static int windowWidth;		//屏幕宽，popwindow占满屏幕宽度
	
	RelativeLayout bottom_service;
	
	Button goto_map; //跳转到地图模式
	static ProApplication application;
	SitesAdapter siteAdapter;
	
	public static final int POLLLIST = 50;
	
	public static List<SiteInfo> currentData = new ArrayList<SiteInfo>();
	
	public static List<SiteInfo> getCurrentData() {
		return currentData;   //传递地图模式使用的
	}
	
	View loadMore;
	ProgressBar progress;
	TextView tv_status;
	
	//地图modern
	static LinearLayout tip;  //提示位置到网点的距离
	static TextView toSiteDistance;  //距离
	static TextView need_time;  //须花费时间
	static int speed = 30;
	
	LinearLayout gotomerchan;  //跳到商家详细
	
	static MapView mMapView = null;
	
	static Resources resources;

	public MKMapViewListener mMapListener = null;
	FrameLayout mMapViewContainer = null;
	MyLocationOverlay myLocationOverlay;
	EditText indexText = null;
	
	
	static int index = -1;

	double pi = 3.1415926;
	// overlay半径
	int r = 50000;

	boolean setCenter = false;

	LayoutInflater factory;

	static View mPopView;

	static TextView popviewTitle = null;
	static RatingBar popviewSubTitle = null;
	static TextView tv_distance = null;
	static ImageView gotoRoute;
	static TextView pop_site_id;


	static boolean animation = false;

	PopupWindow mPopWindow;

	int zoomLevel = 16;
	ProgressDialog progressDialog;
	
	ImageButton left_arrow;
	ImageButton right_arrow;
	TextView from_to;
	TextView siteSum;
	
	// 存放overlayitem
	public List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
	// 存放overlay图片
	public List<Drawable> res = new ArrayList<Drawable>();
	
	OverlayTest2 ov;//覆盖物
	
	boolean sendMessage = false;
	
	int currentPage = 1;	//当前页
	int pageSum = 10;	//一页显示数据数量
	int sum;		//总数据
	int totalPage = 1;   //总页数
	
	
	private String current_distance = "5KM";
	private String current_condition = "全车标准清洗"; 
	private String current_sort = ProApplication.DEFAULT_SORT;

	private int refreshDistance = 500;
	
	//获取网点信息
	public void getSite() {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair curPoint = new BasicNameValuePair("curCoord", application.locData.longitude + "," + application.locData.latitude);
		NameValuePair cityCode = new BasicNameValuePair("areaCode",SharePrefenceUtil.getCityID(ServiceTurnOver1.this));
		NameValuePair disRange = new BasicNameValuePair("disRange",dis);
		NameValuePair serid = new BasicNameValuePair("scId",serviceID);
		NameValuePair ordType = new BasicNameValuePair("orderType",orderType);
		NameValuePair startIndex = new BasicNameValuePair("startIndex",currentData.size() + 1 + "");
		NameValuePair fetchSize = new BasicNameValuePair("fetchSize",FETCHSIZE);
		params.add(disRange);
		params.add(serid);
		params.add(ordType);
		params.add(startIndex);
		params.add(curPoint);
		params.add(cityCode);
		params.add(fetchSize);
		DataLoader loader = new DataLoader(ServiceTurnOver1.this);
		loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.FINDSITE, params, mHandler_list, "", false);
	}
	
	public void getSizeOtherCity1(String location,String areaCode) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		if(areaCode == null || areaCode.equals("")) {
			NameValuePair area = new BasicNameValuePair("areaCode",SharePrefenceUtil.getCityID(ServiceTurnOver1.this));
			params.add(area);
		} else {
			NameValuePair area = new BasicNameValuePair("areaCode",areaCode);
			params.add(area);
		}
		NameValuePair curPoint = new BasicNameValuePair("curCoord", application.locData.longitude + "," + application.locData.latitude);
		NameValuePair disRange = new BasicNameValuePair("disRange","");
		params.add(disRange);
		params.add(curPoint);
		NameValuePair serid = new BasicNameValuePair("scId",serviceID);
		NameValuePair ordType = new BasicNameValuePair("orderType",orderType);
		NameValuePair startIndex = new BasicNameValuePair("startIndex",currentData.size() + 1 + "");
		NameValuePair fetchSize = new BasicNameValuePair("fetchSize",FETCHSIZE);
		
		params.add(serid);
		params.add(ordType);
		params.add(startIndex);
		params.add(fetchSize);
		DataLoader loader = new DataLoader(ServiceTurnOver1.this);
		loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.FINDSITE, params, mHandler_list, "", false);
	}
	
	public void getSizeOtherCity(String location) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair cityCode = new BasicNameValuePair("cityCode",SharePrefenceUtil.getCityID(ServiceTurnOver1.this));
		if(location.equals("")) {
			NameValuePair curPoint = new BasicNameValuePair("curCoord", center);
			NameValuePair disRange = new BasicNameValuePair("disRange","");
			params.add(disRange);
			params.add(curPoint);
		} else {
			NameValuePair disRange = new BasicNameValuePair("disRange","5");
			NameValuePair curPoint = new BasicNameValuePair("curCoord", otherCity_center);
			params.add(disRange);
			params.add(curPoint);
		}
		NameValuePair serid = new BasicNameValuePair("scId",serviceID);
		NameValuePair ordType = new BasicNameValuePair("orderType",orderType);
		NameValuePair startIndex = new BasicNameValuePair("startIndex",currentData.size() + 1 + "");
		NameValuePair fetchSize = new BasicNameValuePair("fetchSize",FETCHSIZE);
		
		params.add(serid);
		params.add(ordType);
		params.add(startIndex);
		params.add(cityCode);
		params.add(fetchSize);
		DataLoader loader = new DataLoader(ServiceTurnOver1.this);
		loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.FINDSITE, params, mHandler_list, "", false);
	}
	
	//若不在本城市，获取其他地区信息
	public void getArea() {
		List<NameValuePair> params = new ArrayList<NameValuePair>();   //SharePrefenceUtil.getCityID(ServiceTurnOver1.this)
		NameValuePair supAreaCode = new BasicNameValuePair("supAreaCode",SharePrefenceUtil.getCityID(ServiceTurnOver1.this));
		params.add(supAreaCode);
		DataLoader loader = new DataLoader(ServiceTurnOver1.this);
		loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.FINDAREA, params, cityArea_handler, "", false);
	}

	//1.首先进去判断默认的缓存有数据没，没有则开始请求，计算总页数，把默认的数据赋值给当前的数据，设置适配器，刷新，若是地图模式则发送消息刷新地图
	//2.若没有数据，则发送请求
	Handler mHandler_list = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == ProApplication.location_cuccess) {
				GeoPoint pre = SharePrefenceUtil.getGeopoint(ServiceTurnOver1.this);
				int latitude = (int)(application.locData.latitude * 1E6);
				int longtitude = (int)(application.locData.longitude * 1E6);
				if(pre != null) {
					GeoPoint cur = new GeoPoint(latitude, longtitude);
					double distance = DistanceUtil.getDistance(pre, cur);
					if(distance > refreshDistance) {
						ProApplication.sort_default.clear();
						ProApplication.sort_distance.clear();
						ProApplication.sort_price_hightolow.clear();
						ProApplication.sort_price_lowtohigh.clear();
						ProApplication.sort_star.clear();
					}
				}
				
				SharePrefenceUtil.setGeopoint(latitude, longtitude, ServiceTurnOver1.this);  //保存数据
				
				if (current_condition.equals(ProApplication.default_condition) && localCity) {

					if (ProApplication.sort_default.size() > 0) {
						if (ProApplication.DEFAULT_SUM % pageSum == 0) {
							totalPage = ProApplication.DEFAULT_SUM / pageSum;
						} else {
							totalPage = ProApplication.DEFAULT_SUM / pageSum
									+ 1;
						}
						sum = ProApplication.DEFAULT_SUM;
						
						currentData.clear();
						currentData.addAll(ProApplication.sort_default);
						siteAdapter.setSites(currentData);
						siteAdapter.notifyDataSetChanged();
						int count = siteAdapter.getCount();
						if (count >= sum) {
							progress.setVisibility(View.GONE);
							tv_status.setText("数据加载完毕");
						}

						if (over_status > 0) { // 地图模式就发送消息
							mHandler_map
									.sendEmptyMessage(ProApplication.location_cuccess);
							sendMessage = true;
						}
					} else {

						if (current_sort.equals(ProApplication.DISTANCE_SORT)) {
							//fetchSize,不传值默认为20;
							orderType = "1";
							getSite();
						} else if (current_sort
								.equals(ProApplication.DEFAULT_SORT)) {
							orderType = "0";
							getSite();
						} else if (current_sort
								.equals(ProApplication.PRICE_SORT_hightolow)) {
							orderType = "3";
							getSite();
						} else if (current_sort
								.equals(ProApplication.PRICE_SORT_lowtohigh)) {
							orderType = "2";
							getSite();
						} else if (current_sort
								.equals(ProApplication.STAR_SORT)) {
							orderType = "4";
							getSite();
						}
					}
				} else if(!localCity){  //根据条件发送请求  //其他城市
					//TODO
					
//					getSizeOtherCity("");
					getSizeOtherCity1("", SharePrefenceUtil.getCityID(ServiceTurnOver1.this));
				} else {
					getSite();
				}
				
			} else if(msg.what == POLLLIST) {
//				loadMore.setVisibility(View.VISIBLE);
					Log.i("bbb", "加载...");
					int count = siteAdapter.getCount();
					if(count >= sum) {
						progress.setVisibility(View.GONE);
						tv_status.setText("数据加载完毕");
					} else {
						//TODO
//						goto_map.setVisibility(View.GONE);
						
						progress.setVisibility(View.VISIBLE);
						tv_status.setText("正在加载数据...");
//						if(currentData.size() != 0) { //
							if(localCity) {
								getSite();
							} else {
//							getSizeOtherCity(otherCity_center);
								getSizeOtherCity1("",otherCity_center);
								
							}
//						}  //
					}
			} else if(msg.what == HttpTask.RESULT_OK) {   //1.发送请求接收到的数据,接卸得到总数据,把得到的数据添加到当前数据集合里面，
				Log.i("aab", "ok---");
				progress.setVisibility(View.VISIBLE);
				tv_status.setText("正在加载数据...");
				
				String json = (String) msg.obj;
				Map map = JsonParser.getSites(json);
				if(map != null) {
					if(map.containsKey("sum")) {
						sum = (Integer) map.get("sum");
					}
					List<SiteInfo> list = (List<SiteInfo>) map.get("siteList");
					if(list != null) {
						currentData.addAll(list);
					}
				}
				
				if(current_distance.equals(ProApplication.default_distance) && current_condition.equals(ProApplication.default_condition)) {
					if(current_sort.equals(ProApplication.DEFAULT_SORT)) {  //2.判断是不是需要缓冲，若需要，则把总数据和数据集合缓存起来
						ProApplication.DEFAULT_SUM = sum;
						ProApplication.sort_default.clear();
						ProApplication.sort_default.addAll(currentData);
					} else if(current_sort.equals(ProApplication.DISTANCE_SORT)) {
						ProApplication.DISTANCE_SUM = sum;
						ProApplication.sort_distance.clear();
						ProApplication.sort_distance.addAll(currentData);
					} else if(current_sort.equals(ProApplication.PRICE_SORT_hightolow)) {
						ProApplication.PRICE_HOGH_LOW_SUM = sum;
						ProApplication.sort_price_hightolow.clear();
						ProApplication.sort_price_hightolow.addAll(currentData);
					} else if(current_sort.equals(ProApplication.PRICE_SORT_lowtohigh)) {
						ProApplication.PRICE_LOW_HIGH_SUM = sum;
						ProApplication.sort_price_lowtohigh.clear();
						ProApplication.sort_price_lowtohigh.addAll(currentData);
					} else if(current_sort.equals(ProApplication.STAR_SORT)) {
						ProApplication.STAR_SUM = sum;
						ProApplication.sort_star.clear();
						ProApplication.sort_star.addAll(currentData);
					}
				}
				
				siteSum.setText("共  " + sum + "  家");
				
				if(sum % pageSum == 0) {
					totalPage = sum/pageSum;
				} else {
					totalPage = sum/pageSum + 1;
				}
				
//				ProApplication.sort_default = new TestData().addData(application.locData);  //要缓存的数据
//				currentData = ProApplication.sort_default;//记录本页面的list数据
				siteAdapter.setSites(currentData);
				
				if(over_status > 0) {  //地图模式就发送消息
					mHandler_map.sendEmptyMessage(ProApplication.location_cuccess);
					sendMessage = true;
				}
				
				
				int count = siteAdapter.getCount();
				if(count >= sum) {
					progress.setVisibility(View.GONE);
					tv_status.setText("数据加载完毕");
				}
				
				siteAdapter.notifyDataSetChanged();
			} else if(msg.what == HttpTask.RESULT_ERROR) {
				
				if(over_status > 0) {  //地图模式就发送消息
					mHandler_map.sendEmptyMessage(ProApplication.location_cuccess);
					sendMessage = true;
				} else {
					//TODO  列表模式
				}
			}
			
			//TODO  加载完毕可以点击
//			goto_map.setVisibility(View.VISIBLE);
			super.handleMessage(msg);
		}
		
	};
	
	Handler mHandler_map = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				int lat = (int) ((ProApplication.getInstance().locData.latitude) * 1E6);
				int lon = (int) ((ProApplication.getInstance().locData.longitude) * 1E6);

				if (!setCenter) {
					mMapView.refresh();
					mMapView.getController().setCenter(new GeoPoint(lat, lon));
					setCenter = true;
				}
			} else if (msg.what == ProApplication.location_cuccess) {
				if (over_status > 0 && sum > 0) { // 地图模式则跳转
					if (mPopView != null) {
						mPopView.setVisibility(View.GONE); // 首先去掉弹出窗口
					}

					if (tip != null) {
						tip.setVisibility(View.GONE);
					}

					
						double latitude = ProApplication.getInstance().locData.latitude;
						double longtitude = ProApplication.getInstance().locData.longitude;
						List<GeoPoint> list = new ArrayList<GeoPoint>(); // 用来排序的list
						GeoPoint myPoint = new GeoPoint((int) (latitude * 1E6),
								(int) (longtitude * 1E6));
						
						siteSum.setText("共  " + sum + "  家");
						
						if(totalPage <= 1) {
							left_arrow.setClickable(false);
							left_arrow.setBackgroundResource(R.drawable.btn_left_disabled);
							
							right_arrow.setClickable(false);
							right_arrow.setBackgroundResource(R.drawable.btn_right_disabled);
							from_to.setText("第  1-" + sum + "  家");
						} else {
							from_to.setVisibility(View.VISIBLE);
							if (currentPage == 1) {
								left_arrow.setClickable(false);
								left_arrow.setBackgroundResource(R.drawable.btn_left_disabled);
								
								right_arrow.setClickable(true);
								right_arrow.setBackgroundResource(R.drawable.map_next_selector);
							} else if (currentPage == totalPage) {
								right_arrow.setClickable(false);
								right_arrow.setBackgroundResource(R.drawable.btn_right_disabled);
								left_arrow.setClickable(true);
								left_arrow.setBackgroundResource(R.drawable.map_pre_selector);
							} else {
								left_arrow.setClickable(true);
								left_arrow.setBackgroundResource(R.drawable.map_pre_selector);
								
								right_arrow.setClickable(true);
								right_arrow.setBackgroundResource(R.drawable.map_next_selector);
							}
							
							if (currentPage == totalPage) {
								from_to.setText("第  " + ((currentPage - 1) * pageSum + 1)
										+ "-" + sum + "  家");
							} else {
								from_to.setText("第  " + ((currentPage - 1) * pageSum + 1)
										+ "-" + currentPage * pageSum +   "  家");
								
							}
						}


						int i = 0;
						
						//清空数据
						mGeoList.clear();
						mMapView.getOverlays().clear();
						
						
						if (mMapView != null) {

						for (SiteInfo site : currentData) {
							if (i < currentPage * pageSum
									&& i >= (currentPage - 1) * pageSum) {
								GeoPoint point = site.getPoint();
								OverlayItem overlayItem = new OverlayItem(
										point, i + "", site.getSiteName());
								overlayItem.setMarker(res.get(0));
								mGeoList.add(overlayItem);
								list.add(point);
							}
							i++;
						}

						Drawable marker = ServiceTurnOver1.this.getResources()
								.getDrawable(R.drawable.green);

						ov = new OverlayTest2(marker, ServiceTurnOver1.this);
						// ov.removeAll();

						for (OverlayItem item1 : mGeoList) {
							ov.addItem(item1);
						}
						mMapView.getOverlays().add(ov);

						if(localCity) {
							list.add(myPoint);
							myLocationOverlay = new MyLocationOverlay(mMapView);
							myLocationOverlay.enableCompass();
							myLocationOverlay.setData(ProApplication.getInstance().locData);
							if (mMapView != null && mMapView.getOverlays() != null) {
								mMapView.getOverlays().add(myLocationOverlay);
							}
						}
						
						//--
						myLocationOverlay = new MyLocationOverlay(mMapView);
						myLocationOverlay.enableCompass();
						myLocationOverlay.setData(ProApplication.getInstance().locData);
						if (mMapView != null && mMapView.getOverlays() != null) {
							mMapView.getOverlays().add(myLocationOverlay);
						}
						
						fitPoints(list);
						
						mMapView.refresh();
					}
				} else {
					if(application != null) {
//						application.getCenter(cityName, mHandler_map);
						Log.i("aaa", "getCenter");
						mHandler_map.sendEmptyMessage(ProApplication.CENTER_OK);
					}
				}
			} else if(msg.what == ProApplication.CENTER_OK) {
				if(mMapView != null && application != null) {
					//清空数据--------------------------
					mGeoList.clear();
					mMapView.getOverlays().clear();
					mMapView.refresh();
					//--------------------------------
					String[] point_center = center.split(",");
					Double latitude = Double.valueOf(point_center[1]);
					Double longtitude = Double.valueOf(point_center[0]);
//					GeoPoint point = application.getCityCenter();
					GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longtitude * 1E6));
					mMapView.getController().animateTo(point);
					Toast.makeText(ServiceTurnOver1.this, "附近没有网点", Toast.LENGTH_LONG).show();
					finishProgress();
					
					//-------------------
					siteSum.setText("共  " + sum + "  家");
					
					if(totalPage <= 1) {
						left_arrow.setClickable(false);
						left_arrow.setBackgroundResource(R.drawable.btn_left_disabled);
						
						right_arrow.setClickable(false);
						right_arrow.setBackgroundResource(R.drawable.btn_right_disabled);
						from_to.setText("第  1-" + sum + "  家");
					} else {
						from_to.setVisibility(View.VISIBLE);
						if (currentPage == 1) {
							left_arrow.setClickable(false);
							left_arrow.setBackgroundResource(R.drawable.btn_left_disabled);
							
							right_arrow.setClickable(true);
							right_arrow.setBackgroundResource(R.drawable.map_next_selector);
						} else if (currentPage == totalPage) {
							right_arrow.setClickable(false);
							right_arrow.setBackgroundResource(R.drawable.btn_right_disabled);
							left_arrow.setClickable(true);
							left_arrow.setBackgroundResource(R.drawable.map_pre_selector);
						} else {
							left_arrow.setClickable(true);
							left_arrow.setBackgroundResource(R.drawable.map_pre_selector);
							
							right_arrow.setClickable(true);
							right_arrow.setBackgroundResource(R.drawable.map_next_selector);
						}
						
						if (currentPage == totalPage) {
							from_to.setText("第  " + ((currentPage - 1) * pageSum + 1)
									+ "-" + sum + "  家");
						} else {
							from_to.setText("第  " + ((currentPage - 1) * pageSum + 1)
									+ "-" + currentPage * pageSum +   "  家");
							
						}
					}
					//-------------------------------------
				}
			}
		};
	};
	
	Handler cityArea_handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == HttpTask.RESULT_OK) {
				String json = (String) msg.obj;
				areaList = JsonParser.getArea(json,cityArea_handler);
				CityArea all = new CityArea();
				all.setCityarea("全城");
				Circle circle = new Circle();
				circle.setCircleName("全城");
				List<Circle> lc = new ArrayList<CityArea.Circle>();
				lc.add(circle);
				all.setCircleList(lc);
				areaList.add(0, all);
			} else if(msg.what == HttpTask.RESULT_ERROR) {
				showToast("获取商圈信息失败");
			} 
			super.handleMessage(msg);
		}
		
	};
	
	
	String typeID = "";
	String serviceID = "";
	List<CityArea> areaList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		application = (ProApplication) getApplication();
		
		Intent intent = getIntent();
		current_condition = intent.getStringExtra("service");
		typeID = intent.getStringExtra("typeID");
		serviceID = intent.getStringExtra("serviceID");
		localCity = intent.getBooleanExtra("localCity", false);
		cityName = intent.getStringExtra("cityName");
		center = intent.getStringExtra("center");
		
		if(!localCity) {
			current_distance = otherCityDefault;
			getArea();
		}
		
		
		Log.i("bbb", current_condition + "---" + typeID + "---" + serviceID + "--");
		currentData.clear();
		
		over_status = SharePrefenceUtil.getRotateStatus(this);
		
		if (application.mBMapManager == null) {
			application.mBMapManager = new BMapManager(this);
			application.mBMapManager.init(ProApplication.strKey,
					new ProApplication.MyGeneralListener());
		}
		
		setContentView(R.layout.turn_over_service_1);
		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.getController().setZoom(zoomLevel);
		mMapView.getController().enableClick(true);

		mMapView.setBuiltInZoomControls(false);  //去掉缩放控件
		
		myLocationOverlay = new MyLocationOverlay(mMapView);
		
		if(application.locData != null) {
			mHandler_list.sendEmptyMessage(ProApplication.location_cuccess);
		} else {
			//定位
			application.startRequest(mHandler_list);
		}
		if(over_status > 0) {
			showProgress("正在加载数据...");
		}
		
		resources = getResources();  //TODO
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		windowWidth = dm.widthPixels;
		
		map_modern = (RelativeLayout) findViewById(R.id.map_modern);
		list_modern = (PollListView) findViewById(R.id.list_all_site);
		mContainer = (ViewGroup) findViewById(R.id.container);
		
		if(over_status > 0) {
			map_modern.setVisibility(View.VISIBLE);
			list_modern.setVisibility(View.GONE);
		} else if(over_status < 0) {
			map_modern.setVisibility(View.GONE);
			list_modern.setVisibility(View.VISIBLE);
		}
		
//		list_modern.setRotationY(-90f);
//		mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
		
		
		initView();
		
		initMapView();

		mMapListener = new MKMapViewListener() {

			@Override
			public void onMapMoveFinish() {
				if (mPopView != null) {
					mPopView.setVisibility(View.GONE);
				}
			}

			@Override
			public void onClickMapPoi(MapPoi mapPoiInfo) {
				// TODO Auto-generated method stub
				String title = "";
				if (mapPoiInfo != null) {
					title = mapPoiInfo.strText;
					// Toast.makeText(LocationOverlayDemo.this,title,Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onGetCurrentMap(Bitmap b) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onMapAnimationFinish() {
				// TODO Auto-generated method stub
				if (animation) {
					ServiceTurnOver1.mPopView.setVisibility(View.VISIBLE);
					animation = false;
				}
			}

		};
		mMapView.regMapViewListener(ProApplication.getInstance().mBMapManager,
				mMapListener);

		res.add(getResources().getDrawable(R.drawable.mark_red));

		factory = getLayoutInflater();
		mPopView = factory.inflate(R.layout.popview_map, null);
		
		gotomerchan = (LinearLayout) mPopView.findViewById(R.id.gotomerchan);
		
		gotoRoute = (ImageView) mPopView.findViewById(R.id.gotoRoute);
		gotoRoute.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				TextView tv_site_id = (TextView) mPopView.findViewById(R.id.pop_site_id);
				String site_id = tv_site_id.getText().toString();
				if(site_id != null && !site_id.equals("")) {
					Intent intent = new Intent(ServiceTurnOver1.this, RoutePlanActivity.class);
					SiteInfo info = ServiceTurnOver1.getCurrentData().get(index);
					GeoPoint points = info.getPoint();
					intent.putExtra("latitude", points.getLatitudeE6());
					intent.putExtra("longtitude", points.getLongitudeE6());
					startActivity(intent);
				}
			}
		});
		
		popviewTitle = (TextView) mPopView.findViewById(R.id.title);
		
		TextPaint paint = popviewTitle.getPaint();
		paint.setFakeBoldText(true);
		popviewSubTitle = (RatingBar) mPopView.findViewById(R.id.subtitle);
		tv_distance = (TextView) mPopView.findViewById(R.id.tv_distance);
		gotoRoute = (ImageView) mPopView.findViewById(R.id.gotoRoute);
		pop_site_id = (TextView) mPopView.findViewById(R.id.pop_site_id);
		
		mMapView.addView(mPopView, new MapView.LayoutParams(
				MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT, null,
				MapView.LayoutParams.TOP_LEFT));
		mPopView.setVisibility(View.GONE);
		
		gotomerchan.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				TextView tv_site_id = (TextView) mPopView.findViewById(R.id.pop_site_id);
				String site_id = tv_site_id.getText().toString();
				if(site_id != null && !site_id.equals("")) {
					Intent intent = new Intent(ServiceTurnOver1.this, MerchantTurnActivity1.class);
					intent.putExtra("siteID", site_id);
					intent.putExtra("sitePosition", index);
					intent.putExtra("typeID", typeID);
					intent.putExtra("serviceID", serviceID);
					intent.putExtra("localCity", localCity);
					
					if(ProApplication.slidingmenu) {
						//slidingmenu模式  TODO
						Intent i = new Intent("com.my");
						i.putExtra("siteID", site_id);
						i.putExtra("sitePosition", index);
						sendBroadcast(i);
					} else {
						//直接跳转模式  TODO
						startActivity(intent);
//						overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
						
					}
					
				}
			}
		});

		mMapView.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (mPopView != null) {
					mPopView.setVisibility(View.GONE);
					tip.setBackgroundColor(Color.parseColor("#00000000"));
					tip.setVisibility(View.INVISIBLE);
					
					if(tap) {
						List<OverlayItem> mGeo = ov.getItem();
						for(int i = 0; i < mGeo.size(); i++) {
							ov.getItem(i).setMarker(res.get(0));
						}
						mMapView.refresh();
						
						tap = false;
					}
//					ov.getItem(arg0)
				}
				return false;
			}
		});
		
	}
	
	boolean locating = false;
	
	private void initView() {
		//设置底部的透明度
		bottom_service = (RelativeLayout) findViewById(R.id.bottom_service);
		bottom_service.getBackground().setAlpha(180);
		
		//设置地图上的放大缩小定位
		dingwei = (ImageView) findViewById(R.id.dingwei);
		if(localCity) {
			dingwei.setVisibility(View.VISIBLE);
		} else {
			dingwei.setVisibility(View.VISIBLE);
		}
		dingwei.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if(application.locData != null) {
					GeoPoint curr_point = new GeoPoint((int)(application.locData.latitude * 1E6), (int)(application.locData.longitude * 1E6));
					mMapView.getController().animateTo(curr_point);
				} else {
					locating = true;
					ProgressDialog progressDialog = new ProgressDialog(ServiceTurnOver1.this);
					progressDialog.setCanceledOnTouchOutside(false);
//					progressDialog.setCancelable(false);
					progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
					progressDialog.setMessage("定位中...");
					progressDialog.show();
					progressDialog.setOnCancelListener(new OnCancelListener() {
						@Override
						public void onCancel(DialogInterface dialog) {
							locating = false;
						}
					});
					application.startRequest(new Handler() {
						@Override
						public void handleMessage(Message msg) {
							if(msg.what == ProApplication.location_fial) {
								showToast("定位失败");
							} else if(msg.what == ProApplication.location_cuccess){
								if(locating && !isFinishing()) {
									GeoPoint curr_point = new GeoPoint((int)(application.locData.latitude * 1E6), (int)(application.locData.longitude * 1E6));
									mMapView.getController().animateTo(curr_point);
								}
							}
							super.handleMessage(msg);
						}
					});
				}
			}
		});
		zoom_out = (ImageView) findViewById(R.id.small);
		zoom_in = (ImageView) findViewById(R.id.big);
		zoom_out.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				mMapView.getController().zoomOut();	
			}
		});
		
		zoom_in.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mMapView.getController().zoomIn();
			}
		});
		
		re_distanse_click = (RelativeLayout) findViewById(R.id.re_distanse_click);
		re_condiction_click = (RelativeLayout) findViewById(R.id.re_condiction_click);
		re_sort_click = (RelativeLayout) findViewById(R.id.re_sort_click);
		re_distanse_click.setOnClickListener(this);
		re_condiction_click.setOnClickListener(this);
		re_sort_click.setOnClickListener(this);
		
		chosed_distance = (TextView) findViewById(R.id.chose_distance);
		if(!localCity) {
			chosed_distance.setText(otherCityDefault);   //TODO
		} else {
			chosed_distance.setText("5KM");
		}
		distance_arrow = (ImageView) findViewById(R.id.distance_arrow);
		
		condiction_arrow = (ImageView) findViewById(R.id.condiction_arrow);
		chosed_service_type = (TextView) findViewById(R.id.chose_service_type);
		chosed_service_type.setText(current_condition);
		
		chosed_sort = (TextView) findViewById(R.id.chose_sort);
		chosed_sort.setText(ProApplication.DEFAULT_SORT);
		sort_arrow = (ImageView) findViewById(R.id.sort_arrow);
		
		final AnimationController controller = new AnimationController();
		
		goto_map = (Button) findViewById(R.id.goto_map);
		if(over_status < 0) {
			goto_map.setBackgroundResource(R.drawable.map);
		} else if(over_status > 0) {
			goto_map.setBackgroundResource(R.drawable.list);
		}
		//TODO 设置不可切换模式
//		goto_map.setVisibility(View.GONE);
		
		goto_map.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				flipit();
				if(over_status < 0) {  //当前列表，跳转到地图
//					applyRotation(-1, 360, 270);
					over_status = 1;
					
					controller.fadeOut(list_modern, 500, 0);
					controller.fadeIn(map_modern, 500, 300);  //TODO
					
					//默认是列表时，跳转到地图模式，动画结束后发送消息
//		            if(!sendMessage) {
					if(!sendMessage) {  //第一次进入是列表时，跳转到地图模式显示风火轮
						showProgress("正在加载数据...");
						sendMessage = true;
					}
						mHandler_map.sendEmptyMessage(ProApplication.location_cuccess);
						
						goto_map.setBackgroundResource(R.drawable.list);
					
				} else if(over_status > 0) {  //当前地图，跳转到列表
//					applyRotation(1, 0, 90);
					over_status = -1;
					
					controller.fadeOut(map_modern, 500, 0);
					controller.fadeIn(list_modern, 500, 300);  //TODO
					
					goto_map.setBackgroundResource(R.drawable.map);
					sites_list.setSelection(0);
					
				}
			}
		});
		
		
		sites_list = (PollListView) findViewById(R.id.list_all_site);
		sites_list.setHandler(mHandler_list);
		
		loadMore = getLayoutInflater().inflate(R.layout.progressbar, null);
		progress = (ProgressBar) loadMore.findViewById(R.id.progress);
		tv_status = (TextView) loadMore.findViewById(R.id.tv_status);
		sites_list.addFooterView(loadMore);  //加载更多
		
		siteAdapter = new SitesAdapter(getLayoutInflater(),localCity);
		sites_list.setAdapter(siteAdapter);
		
		sites_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Intent intent = new Intent(ServiceTurnOver1.this, MerchantTurnActivity1.class);
//				long site_id = parent.getAdapter().getItemId(position);
				TextView tv_site = (TextView) view.findViewById(R.id.site_id);
				if(tv_site != null) {
					
					String site_id = tv_site.getText().toString();
					intent.putExtra("siteID", site_id);
					intent.putExtra("sitePosition", position);
					intent.putExtra("typeID", typeID);
					intent.putExtra("serviceID", serviceID);
					intent.putExtra("localCity", localCity);
					
					if(ProApplication.slidingmenu) {
						//slidingMenu 模式  TODO
						Intent i = new Intent("com.my");
						i.putExtra("siteID", site_id);
						i.putExtra("sitePosition", position);
						sendBroadcast(i);
					} else {
						//直接跳转模式 TODO
						startActivity(intent);  
//						overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
					}
					
				}
			}
		});
		
		
		
	}
	
	public void initRadio(View view) {
		radio1 = (RadioButton) view.findViewById(R.id.radio_1);
		radio2 = (RadioButton) view.findViewById(R.id.radio_2);
		radio3 = (RadioButton) view.findViewById(R.id.radio_3);
		radio4 = (RadioButton) view.findViewById(R.id.radio_4);
		
		radio1.setOnClickListener(this);
		radio2.setOnClickListener(this);
		radio3.setOnClickListener(this);
		radio4.setOnClickListener(this);
		
	}
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.re_distanse_click:
				distance_arrow.setImageResource(R.drawable.ic_arrow_up_black);
				re_distanse_click.setSelected(true);
				re_condiction_click.setSelected(false);
				re_sort_click.setSelected(false);
				
				if(localCity) {
					View mDistance = getLayoutInflater().inflate(
							R.layout.pop_distance, null);
					initRadio(mDistance);
					String chosed_distance_text = chosed_distance.getText()
							.toString();
					if (chosed_distance_text.equals(radio1.getText().toString())) {
						radio1.setChecked(true);
					} else if (chosed_distance_text.equals(radio2.getText()
							.toString())) {
						radio2.setChecked(true);
					} else if (chosed_distance_text.equals(radio3.getText()
							.toString())) {
						radio3.setChecked(true);
					} else if (chosed_distance_text.equals(radio4.getText()
							.toString())) {
						radio4.setChecked(true);
					}
					
					if (mPopWindow_distance == null) {
						mPopWindow_distance = new PopupWindow(mDistance,
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT);
						
						mPopWindow_distance.setWidth(windowWidth);
						
						mPopWindow_distance.setOutsideTouchable(true);
						mPopWindow_distance.setFocusable(true);
						
						mPopWindow_distance.setTouchInterceptor(new OnTouchListener() {
							@Override
							public boolean onTouch(View v, MotionEvent event) {
								float x = event.getX();
								float y = event.getY();
								
								if(x > re_condiction_click.getLeft() && x < re_condiction_click.getRight() && y < re_condiction_click.getTop() && y > -re_condiction_click.getBottom()) {
									re_condiction_click.performClick();
								} else if(x > re_sort_click.getLeft() && x < re_sort_click.getRight() && y < re_sort_click.getTop() && y > -re_sort_click.getBottom()) {
									re_sort_click.performClick();
								}
								return false;
							}
						});
						
						// 让pop可以点击外面消失掉
						mPopWindow_distance
						.setBackgroundDrawable(new ColorDrawable(0));
						
						mPopWindow_distance
						.setOnDismissListener(new OnDismissListener() {
							@Override
							public void onDismiss() {
								re_distanse_click.setSelected(false);
								distance_arrow
								.setImageResource(R.drawable.ic_arrow_down_black);
							}
						});
						
					} else {
						mPopWindow_distance.setContentView(mDistance);
					}
					mPopWindow_distance.showAsDropDown(re_distanse_click, 0, 0);
					mPopWindow_distance.update();
				} else {
					View mCondition = getLayoutInflater().inflate(
							R.layout.pop_condiction, null);
					initCityArea(mCondition);
					
					if (mPopWindow_distance == null) {
						mPopWindow_distance = new PopupWindow(mCondition,
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT);
						
						//设置宽和高
						mPopWindow_distance.setWidth(screenWidth);
						if(screenWidth == 480) {
							mPopWindow_distance.setHeight(450);
						} else if(screenWidth == 720 || screenWidth == 800) {
							mPopWindow_distance.setHeight(700);
						} else {
							mPopWindow_distance.setHeight(550);
						}
						
						mPopWindow_distance.setOutsideTouchable(true);
						mPopWindow_distance.setFocusable(true);
						
						mPopWindow_distance.setTouchInterceptor(new OnTouchListener() {
							@Override
							public boolean onTouch(View view, MotionEvent event) {
								float x = event.getX();
								float y = event.getY();
								
								if(x > re_condiction_click.getLeft() && x < re_condiction_click.getRight() && y < re_condiction_click.getTop() && y > -re_condiction_click.getBottom()) {
									re_condiction_click.performClick();
								} else if(x > re_sort_click.getLeft() && x < re_sort_click.getRight() && y < re_sort_click.getTop() && y > -re_sort_click.getBottom()) {
									re_sort_click.performClick();
								}
								return false;
							}
						});
						
						// 让pop可以点击外面消失掉
						mPopWindow_distance
								.setBackgroundDrawable(new ColorDrawable(0));

						mPopWindow_distance
								.setOnDismissListener(new OnDismissListener() {
									@Override
									public void onDismiss() {
										re_distanse_click.setSelected(false);
										distance_arrow
										.setImageResource(R.drawable.ic_arrow_down_black);
									}
								});
					} else {
						mPopWindow_distance.setContentView(mCondition);
					}
					mPopWindow_distance.showAsDropDown(re_distanse_click, 0, 0);
					mPopWindow_distance.update();
				}
			
			break;
		
		case R.id.re_condiction_click:
			condiction_arrow.setImageResource(R.drawable.ic_arrow_up_black);
			re_condiction_click.setSelected(true);
			re_sort_click.setSelected(false);
			re_distanse_click.setSelected(false);
			
			View mCondition = getLayoutInflater().inflate(
					R.layout.pop_condiction, null);
			initCondition(mCondition);
			
			if (mPopWindow_condition == null) {
				mPopWindow_condition = new PopupWindow(mCondition,
						LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT);
				
				//设置宽和高
				mPopWindow_condition.setWidth(screenWidth);
				if(screenWidth == 480) {
					mPopWindow_condition.setHeight(400);
				} else if(screenWidth == 540){
					mPopWindow_condition.setHeight(400);
				} else {
					mPopWindow_condition.setHeight(700);
				}
				
				mPopWindow_condition.setOutsideTouchable(true);
				mPopWindow_condition.setFocusable(true);
				
				mPopWindow_condition.setTouchInterceptor(new OnTouchListener() {
					@Override
					public boolean onTouch(View view, MotionEvent event) {
						float x = event.getX();
						float y = event.getY();
						if(x > re_distanse_click.getLeft() && x < re_distanse_click.getRight() && y < re_distanse_click.getTop() && y > -re_distanse_click.getBottom()) {
							re_distanse_click.performClick();
						} else if(x > re_sort_click.getLeft() && x < re_sort_click.getRight() && y < re_sort_click.getTop() && y > -re_sort_click.getBottom()) {
							re_sort_click.performClick();
						}
						return false;
					}
				});
				
				// 让pop可以点击外面消失掉
				mPopWindow_condition
						.setBackgroundDrawable(new ColorDrawable(0));

				mPopWindow_condition
						.setOnDismissListener(new OnDismissListener() {
							@Override
							public void onDismiss() {
								re_condiction_click.setSelected(false);
								condiction_arrow
										.setImageResource(R.drawable.ic_arrow_down_black);
							}
						});
			} else {
				mPopWindow_condition.setContentView(mCondition);
			}
			mPopWindow_condition.showAsDropDown(re_condiction_click, 0, 0);
			mPopWindow_condition.update();
			
			break;
			
		case R.id.re_sort_click:
			sort_arrow.setImageResource(R.drawable.ic_arrow_up_black);
			re_sort_click.setSelected(true);
			re_distanse_click.setSelected(false);
			re_condiction_click.setSelected(false);
			
			View mSort = getLayoutInflater().inflate(
					R.layout.pop_sort, null);
			initSort(mSort);
			
			if (mPopWindow_sort == null) {
				mPopWindow_sort = new PopupWindow(mSort,
						LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT);
				
				mPopWindow_sort.setWidth(windowWidth);
				
				mPopWindow_sort.setOutsideTouchable(true);
				mPopWindow_sort.setFocusable(true);
				
				mPopWindow_sort.setWidth(screenWidth);
//				mPopWindow_sort.setHeight(550);
				
				mPopWindow_sort.setTouchInterceptor(new OnTouchListener() {
					
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						float x = event.getX();
						float y = event.getY();
						if(x < re_condiction_click.getRight() && x > re_condiction_click.getLeft() && y < re_condiction_click.getTop() && y > -re_condiction_click.getBottom()) {
//							re_condiction_click.callOnClick();  //api 11以上才能
							re_condiction_click.performClick();
						} else if(event.getX() < re_distanse_click.getRight() && event.getX() > re_distanse_click.getLeft() && y < re_distanse_click.getTop() && y > -re_distanse_click.getBottom()) {
							re_distanse_click.performClick();
						}
						return false;
					}
				});
				
				// 让pop可以点击外面消失掉
				mPopWindow_sort
						.setBackgroundDrawable(new ColorDrawable(0));

				mPopWindow_sort
						.setOnDismissListener(new OnDismissListener() {
							@Override
							public void onDismiss() {
								re_sort_click.setSelected(false);
								sort_arrow
										.setImageResource(R.drawable.ic_arrow_down_black);
							}
						});
			} else {
				mPopWindow_sort.setContentView(mSort);
			}
			mPopWindow_sort.showAsDropDown(re_sort_click, 0, 0);
			mPopWindow_sort.update();
			break;
			
		case R.id.radio_1:
			chosed_distance.setText(radio1.getText().toString());
			if(mPopWindow_distance != null && mPopWindow_distance.isShowing()) {
				mPopWindow_distance.dismiss();
			}
			
			dis = "5";
			switchData();
			
			break;
			
		case R.id.radio_2:
			chosed_distance.setText(radio2.getText().toString());
			if(mPopWindow_distance != null && mPopWindow_distance.isShowing()) {
				mPopWindow_distance.dismiss();
			}
			dis = "10";
			switchDataNoCache();
			break;
			
		case R.id.radio_3:
			dis = "20";
			chosed_distance.setText(radio3.getText().toString());
			if(mPopWindow_distance != null && mPopWindow_distance.isShowing()) {
				mPopWindow_distance.dismiss();
			}
			
			switchDataNoCache();
			break;
			
		case R.id.radio_4:
			dis = "";
			chosed_distance.setText(radio4.getText().toString());
			if(mPopWindow_distance != null && mPopWindow_distance.isShowing()) {
				mPopWindow_distance.dismiss();
			}
			
			switchDataNoCache();
			
			break;

		default:
			break;
		}
		
	}


	public boolean judgeCache() {
		boolean cache = false;
		//若使用缓存，则得到sum和当前数据,计算totalPage
		if(current_condition.equals(ProApplication.default_condition) && current_distance.equals(ProApplication.default_distance)) {
			if(current_sort.equals(ProApplication.DEFAULT_SORT)) {
				sum = ProApplication.DEFAULT_SUM;
				currentData.clear();
				currentData.addAll(ProApplication.sort_default);
			} else if(current_sort.equals(ProApplication.DISTANCE_SORT)) {
				sum = ProApplication.DISTANCE_SUM;
				currentData.clear();
				currentData.addAll(ProApplication.sort_distance);
			} else if(current_sort.equals(ProApplication.PRICE_SORT_hightolow)) {
				sum = ProApplication.PRICE_HOGH_LOW_SUM;
				currentData.clear();
				currentData.addAll(ProApplication.sort_price_hightolow);
			} else if(current_sort.equals(ProApplication.PRICE_SORT_lowtohigh)) {
				sum = ProApplication.PRICE_LOW_HIGH_SUM;
				currentData.clear();
				currentData.addAll(ProApplication.sort_price_lowtohigh);
			} else if(current_sort.equals(ProApplication.STAR_SORT)) {
				sum = ProApplication.STAR_SUM;
				currentData.clear();
				currentData.addAll(ProApplication.sort_star);
			}
			if(sum % pageSum == 0) {
				totalPage = sum/pageSum;
			} else {
				totalPage = sum/pageSum + 1;
			}
			cache = true;
		} 
		return cache;
	}
	
	public void switchData() {
		progress.setVisibility(View.VISIBLE);	//列表的风火轮显示可见
		tv_status.setText("正在加载数据...");		//显示加载中字样
		
		//首先得到条件
		current_condition = chosed_service_type.getText().toString();
		current_distance = chosed_distance.getText().toString();
		current_sort = chosed_sort.getText().toString();
		
		sum = 0;  //总数据清空
		totalPage = 1;  //总页数清空
		currentPage = 1;	//当前页数变为1
		siteAdapter.setCurrentPage(currentPage);
		currentData.clear();
		
		//刷新数据，首先判断是否使用缓存
		if(!judgeCache()) {   //不使用缓存的
			//TODO发送请求
			if (over_status > 0) {  //地图模式显示风火轮，不使用缓存则需要网络请求，显示风火轮
				showProgress("正在加载数据...");
			}
			
			if(localCity) {
				getSite();
			} else {
				//TODO  其他城市
//				getSizeOtherCity(otherCity_center);
				getSizeOtherCity1("",otherCity_center);
			}
		} else {   //使用缓存
			siteAdapter.setSites(currentData);  //前面判断的时候已经给currentData赋值，
			if(currentData.size() == 0) {  //缓存没数据
				if (over_status > 0) {
					showProgress("正在加载数据...");
				}															//若缓存没有数据，则像服务器请求数据
				if(current_sort.equals(ProApplication.DISTANCE_SORT)) {
					orderType = "1";
					getSite();
				} else if(current_sort.equals(ProApplication.DEFAULT_SORT)) {
					orderType = "0";
					getSite();
				} else if(current_sort.equals(ProApplication.PRICE_SORT_hightolow)) {
					orderType = "3";
					getSite();
				} else if(current_sort.equals(ProApplication.PRICE_SORT_lowtohigh)) {
					orderType = "2";
					getSite();
				} else if(current_sort.equals(ProApplication.STAR_SORT)) {
					orderType = "4";
					getSite();
				}
			} else {	//缓存有数据，直接跳转,sum,totalpage，currentpage都已经赋值
				mHandler_map.sendEmptyMessage(ProApplication.location_cuccess);
				
				int count = siteAdapter.getCount();
				if(count >= sum) {
					progress.setVisibility(View.GONE);
					tv_status.setText("数据加载完毕");
				}
			}
			siteAdapter.notifyDataSetChanged();  //刷新列表
		}
		
		
			list_modern.setSelection(0);
			
		
		
	}
	
	public void switchDataNoCache() {
		progress.setVisibility(View.VISIBLE);	//列表的风火轮显示可见
		tv_status.setText("正在加载数据...");		//显示加载中字样
		
		current_condition = chosed_service_type.getText().toString();
		current_distance = chosed_distance.getText().toString();
		current_sort = chosed_sort.getText().toString();
		
		sum = 0;  //总数据清空
		totalPage = 1;  //总页数清空
		currentPage = 1;	//当前页数变为1
		siteAdapter.setCurrentPage(currentPage);
		currentData.clear();
		
		if (over_status > 0) {
			showProgress("正在加载数据...");
		}
		if(localCity) {
			getSite();
		} else {
//			getSizeOtherCity(otherCity_center);
			getSizeOtherCity1("",otherCity_center);
		}
		list_modern.setSelection(0);
	}


	private void initSort(View mSort) {
		String sortSelected = chosed_sort.getText().toString();
		ListView list_sort = (ListView) mSort.findViewById(R.id.list_sort);
		final PopSortAdapter sortAdapter = new PopSortAdapter(getLayoutInflater(),sortSelected);
		list_sort.setAdapter(sortAdapter);
		
		list_sort.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterview, View view,
					int i, long l) {
				if(mPopWindow_sort != null) {
					mPopWindow_sort.dismiss();
				}
				if(i == 3) {
					orderType = "4";
				} else {
					orderType = i + "";  //排序
				}
				chosed_sort.setText(sortAdapter.getItem(i).toString());
					
				switchData();	
				
			}
		});
	}

	private void initCondition(View mCondition) {
		ListView list_service_type = (ListView) mCondition.findViewById(R.id.list_service_type);
		final ListView list_service = (ListView) mCondition.findViewById(R.id.list_service);
//		list_service.setBackgroundColor(Color.parseColor("#505050"));
		List<KuanterService> kuanterService = application.getList_kuanterService();
		
		final PopServiceTypeAdapter serviceTypeAdapter = new PopServiceTypeAdapter(getLayoutInflater());
		serviceTypeAdapter.setServiceData(kuanterService);
		
		kuanterService = serviceTypeAdapter.getServiceData();
		
		list_service_type.setAdapter(serviceTypeAdapter);
		list_service_type.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
//				view.setSelected(true);
				PopServiceTypeAdapter.selected = position;
				serviceTypeAdapter.notifyDataSetChanged();
				KuanterService ks = (KuanterService) serviceTypeAdapter.getItem(position);
				List<Service> list = ks.getList_service();
				PopServiceAdapter serviceAdapter = new PopServiceAdapter(getLayoutInflater(),list);
				list_service.setAdapter(serviceAdapter);
			}
		});
		
		int service_selected = -1;
		
		List<Service > pop_list = null;
		String tx_service = chosed_service_type.getText().toString();
		if(kuanterService != null) {
			
			for(int i = 0; i < kuanterService.size(); i++) {
				List<Service> list = kuanterService.get(i).getList_service();
				if(list != null) {
					for(int j = 0; j < list.size(); j++) {
						if(list.get(j).getServiceName().contains(tx_service)) {
							PopServiceTypeAdapter.selected = i;
							list_service_type.setSelection(i);
							service_selected = j;
							pop_list = list;
							break;
						}
					}
				}
			}
			
		}
		PopServiceAdapter serviceAdapter = new PopServiceAdapter(getLayoutInflater(),pop_list);
		serviceAdapter.setService_selected(service_selected);
		list_service.setAdapter(serviceAdapter);
		if(service_selected >= 0) {
			list_service.setSelection(service_selected);
		}
		
		
		list_service.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Service service_click = (Service) parent.getAdapter().getItem(position);
				chosed_service_type.setText(service_click.getServiceName());
				serviceID = service_click.getServiceID();
				typeID = service_click.getParentId();
				if(mPopWindow_condition != null) {
					mPopWindow_condition.dismiss();
				}
				
				switchData();
				
			}
		});
	}
	
	private void initCityArea(View mCondition) {
		ListView list_area = (ListView) mCondition.findViewById(R.id.list_service_type);
		final ListView list_circle = (ListView) mCondition.findViewById(R.id.list_service);
//		list_service.setBackgroundColor(Color.parseColor("#505050"));
		
		final CityAreaAdapter cityAreaAdapter = new CityAreaAdapter(getLayoutInflater());
		cityAreaAdapter.setServiceData(areaList);
		list_area.setAdapter(cityAreaAdapter);
		list_area.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
//				view.setSelected(true);
				CityAreaAdapter.selected_area = position;
				cityAreaAdapter.notifyDataSetChanged();
				CityArea ks = (CityArea) cityAreaAdapter.getItem(position);
				List<Circle> list = ks.getCircleList();
				CityCircleAdapter circleAdapter = new CityCircleAdapter(getLayoutInflater(),list);
				list_circle.setAdapter(circleAdapter);
			}
		});
		
		int circle_selected = -1;
		
		List<Circle> pop_list = null;
		String tx_circle = chosed_distance.getText().toString();
		if(areaList != null) {
			
			for(int i = 0; i < areaList.size(); i++) {
				List<Circle> list = areaList.get(i).getCircleList();
				if(list != null) {
					for(int j = 0; j < list.size(); j++) {
						if(list.get(j).getCircleName().equals(tx_circle)) {
							CityAreaAdapter.selected_area = i;
							list_area.setSelection(i);
							circle_selected = j;
							pop_list = list;
							break;
						}
					}
				}
			}
			
		}
		CityCircleAdapter ccAdapter = new CityCircleAdapter(getLayoutInflater(),pop_list);
		ccAdapter.setCircle_selected(circle_selected);
		list_circle.setAdapter(ccAdapter);
		if(circle_selected >= 0) {
			list_circle.setSelection(circle_selected);
		}
		
		
		list_circle.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Circle circle = (Circle)parent.getAdapter().getItem(position);
				chosed_distance.setText(circle.getCircleName());
				if(mPopWindow_distance != null) {
					mPopWindow_distance.dismiss();
				}
//				otherCity_center = circle.getGpsCoordinate();
				otherCity_center = circle.getCircleID();
				if(otherCity_center == null) {
					otherCity_center = "";
				}
				
				//地区中心点
				center = circle.getGpsCoordinate();
				if(center == null) {
					center = SharePrefenceUtil.getCenter(ServiceTurnOver1.this);
				}
				switchData();
				
			}
		});
	}



	//按下返回键
	public void exit(View view) {
		finish();
//		overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out);
	}
	
	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		if(mMapView != null) {
			mMapView.onPause();
		}
		super.onPause();
	}
	
	

	Handler handler_service = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == HttpTask.RESULT_OK) {
				String json = (String) msg.obj;
				List<KuanterService> service = JsonParser.getKuanterService(json);
				application.setList_kuanterService(service);  
			}
			super.handleMessage(msg);
		}
		
	};
	
	@Override
	protected void onResume() {
		MobclickAgent.onResume(this);
		if(mMapView != null) {
			mMapView.onResume();
		}
		
		CheckNetworkState();
		if(application.getList_kuanterService() == null) {
			DataLoader loader = new DataLoader(ServiceTurnOver1.this);
			List<NameValuePair> params1 = new ArrayList<NameValuePair>();
			NameValuePair cityCode = new BasicNameValuePair("cityCode",SharePrefenceUtil.getCityID(ServiceTurnOver1.this));
			params1.add(cityCode);
			loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.FINDSERVICE, params1, handler_service, "", false);
		} 
		
		if(currentData == null || currentData.size() == 0) {
//			getSizeOtherCity1("", SharePrefenceUtil.getCityID(ServiceTurnOver1.this));
		} else {
			if(siteAdapter != null) {
				siteAdapter.notifyDataSetChanged();
			}
			if(over_status > 0) {
				mHandler_map.sendEmptyMessage(ProApplication.location_cuccess);
			}
		}
		if(areaList == null || areaList.size() == 0) {
			getArea();
		}
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if(mMapView != null) {
//			mMapView.destroy();
		}
		finishProgress();
		super.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if(mMapView != null) {
			mMapView.onSaveInstanceState(outState);
		}

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if(mMapView != null) {
			mMapView.onRestoreInstanceState(savedInstanceState);
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
//			overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out);
		}
		return super.onKeyDown(keyCode, event);
	}

	//初始化地图模式上面的距离view
	private void initMapView() {
		mMapView.setLongClickable(true);
		// mMapController.setMapClickEnable(true);
		// mMapView.setSatellite(false);
		
		tip = (LinearLayout) findViewById(R.id.tip);
		toSiteDistance = (TextView) findViewById(R.id.toSiteDistance);
		need_time = (TextView) findViewById(R.id.need_time);
		
		left_arrow = (ImageButton) findViewById(R.id.left_arrow);
		left_arrow.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				currentPage--;
				if(currentPage <= 0) { 
					currentPage = 1;
				}
				mHandler_map.sendEmptyMessage(ProApplication.location_cuccess);
			}
		});
		
		right_arrow = (ImageButton) findViewById(R.id.right_arrow);
		right_arrow.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(progressDialog == null || !progressDialog.isShowing()) {
					currentPage++;
					if(currentPage >= totalPage) {
						currentPage = totalPage;
					}
					if(((currentPage - 1) * pageSum + 1) > currentData.size()) {  	// && currentPage != totalPage
						showProgress("正在加载数据...");
						if(localCity) {
							getSite();
						} else {
//							getSizeOtherCity(otherCity_center);
							getSizeOtherCity1("",otherCity_center);
						}
					} else {
						mHandler_map.sendEmptyMessage(ProApplication.location_cuccess);
					}
				}
			}
		});
		
		from_to = (TextView) findViewById(R.id.form_to);
		siteSum = (TextView) findViewById(R.id.siteSum);
		
		
	}

	protected void showProgress(String message) {
		progressDialog = new ProgressDialog(ServiceTurnOver1.this);
		progressDialog.setCanceledOnTouchOutside(false);
//		progressDialog.setCancelable(false);
		progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		progressDialog.setMessage(message);
		progressDialog.show();
		progressDialog.setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				finish();
			}
		});
	}
	
	/**
	 * 结束进度框
	 * 
	 * @param progress
	 */
	protected void finishProgress() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog=null;
		}
	}
	
	
	//地图缩放
	private void fitPoints(List<GeoPoint> points) {
	    // set min and max for two points
	    int nwLat = -90 * 1000000;
	    int nwLng = 180 * 1000000;
	    int seLat = 90 * 1000000;
	    int seLng = -180 * 1000000;
	    // find bounding lats and lngs
	    for (GeoPoint point : points) {
	        nwLat = Math.max(nwLat, point.getLatitudeE6());
	        nwLng = Math.min(nwLng, point.getLongitudeE6());
	        seLat = Math.min(seLat, point.getLatitudeE6());
	        seLng = Math.max(seLng, point.getLongitudeE6());
	    }
	    GeoPoint center = new GeoPoint((nwLat + seLat) / 2, (nwLng + seLng) / 2);
	    // add padding in each direction
	    final int spanLatDelta = (int) (Math.abs(nwLat - seLat) * 1.1);
	    final int spanLngDelta = (int) (Math.abs(seLng - nwLng) * 1.1);
//	    int spanLatDelta = nwLat - seLat;
//	    int spanLngDelta = seLng - nwLng;

	    // fit map to points
	    mMapView.getController().animateTo(center);
//	    mMapView.getController().zoomToSpan(spanLatDelta, spanLngDelta);
	    mHandler_map.postDelayed(new Runnable() {
    		@Override
    		public void run() {
    			if(mMapView != null) {
    				if(mMapView.getController() != null) {
    					mMapView.getController().zoomToSpan(spanLatDelta, spanLngDelta);
    				}
    			}
    			finishProgress();
    		}
    	}, 1000);
	    
	}
	
}

class OverlayTest2 extends ItemizedOverlay<OverlayItem> {
	public List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
	private Context mContext = null;
	static PopupOverlay pop = null;

	Toast mToast = null;

	public OverlayTest2(Drawable marker, Context context) {
		super(marker);
		this.mContext = context;
		pop = new PopupOverlay(ServiceTurnOver1.mMapView,
				new PopupClickListener() {

					@Override
					public void onClickedPopup(int index) {
						if (null == mToast)
							mToast = Toast.makeText(mContext, "popup item :"
									+ index + " is clicked.",
									Toast.LENGTH_SHORT);
						else
							mToast.setText("popup item :" + index
									+ " is clicked.");
						mToast.show();
					}
				});
		populate();

	}

	GeoPoint prePoint;  //表示是前一个地理位置，当第二次点击的地点位置没改变时不会触发animation，所以弹出不了popwindow，一次在这判断两次位置，若相同，在显示popwindow
	int i = 0;
	protected boolean onTap(int index) {
		String title = mGeoList.get(index).getTitle();
		if(!title.equals("my")) {
			int location = Integer.valueOf(title);
			ServiceTurnOver1.index = location;
			if(ServiceTurnOver1.currentData != null) {
				SiteInfo site = ServiceTurnOver1.currentData.get(location);
				String siteName = site.getSiteName();
			/*	if(siteName.contains("(")) {
					String[] sites= siteName.split("(");
					siteName = sites[0];
				} else if(siteName.contains("（")) {
					String[] sites= siteName.split("（");
					siteName = sites[0];
				}*/
				ServiceTurnOver1.popviewTitle.setText(siteName);
				ServiceTurnOver1.tv_distance.setText(site.getMinServicePrice() + "~" + site.getMaxServicePrice() + "积分");
				ServiceTurnOver1.tv_distance.setVisibility(View.VISIBLE);
				ServiceTurnOver1.popviewSubTitle.setVisibility(View.VISIBLE);
				ServiceTurnOver1.gotoRoute.setVisibility(View.VISIBLE);
				ServiceTurnOver1.pop_site_id.setText(site.getId() + "");
				
				String score = site.getScore();
				//评分
				float f_score = Float.valueOf(score);
				ServiceTurnOver1.popviewSubTitle.setRating(f_score);
				
				//提示距离
				if(ServiceTurnOver1.localCity) {
					String my_distance = site.getSiteDistance();
					double km = Double.valueOf(my_distance);
					long ll = Math.round(km * 100);
					km= ll / 100.0;
//					float[] distance = new float[1];
//					Location.distanceBetween(ServiceTurnOver1.application.locData.latitude, ServiceTurnOver1.application.locData.longitude, mGeoList.get(index).getPoint().getLatitudeE6()/1000000.00, mGeoList.get(index).getPoint().getLongitudeE6()/1000000.00, distance);
//					int km = (int) (distance[0] / 1000);
					int minute = (int) ((km * 60) / ServiceTurnOver1.speed);
					ServiceTurnOver1.toSiteDistance.setText(km + "KM");
					ServiceTurnOver1.tip.setBackgroundColor(Color.BLACK);
//					ServiceTurnOver1.tip.setVisibility(View.VISIBLE);	//
					ServiceTurnOver1.tip.setVisibility(View.GONE);
					if(minute == 0) {
						ServiceTurnOver1.need_time.setText("<1分钟");
					} else {
						ServiceTurnOver1.need_time.setText(minute + "分钟");
					}
				}
			}
		} else {
			ServiceTurnOver1.index = -1;
			ServiceTurnOver1.popviewTitle.setText("我的位置");
			ServiceTurnOver1.tv_distance.setVisibility(View.GONE);
			ServiceTurnOver1.popviewSubTitle.setVisibility(View.GONE);
			ServiceTurnOver1.gotoRoute.setVisibility(View.GONE);
			ServiceTurnOver1.pop_site_id.setText("");
		}
		

		// 更新气泡位置,并使之显示
		GeoPoint pt = mGeoList.get(index).getPoint();
		
		if(ServiceTurnOver1.windowWidth == 720 || ServiceTurnOver1.windowWidth == 800) {
			ServiceTurnOver1.mMapView.updateViewLayout(
					ServiceTurnOver1.mPopView, new MapView.LayoutParams(
							608, 173,
							pt, 0,
							0 - 78,				//ServiceTurnOver1.mPopView.getHeight()/3
							MapView.LayoutParams.BOTTOM_CENTER));
		} else if(ServiceTurnOver1.windowWidth == 480) {
			ServiceTurnOver1.mMapView.updateViewLayout(
					ServiceTurnOver1.mPopView, new MapView.LayoutParams(
							405, 115,
							pt, 0,
							0 - 61,				//ServiceTurnOver1.mPopView.getHeight()/3
							MapView.LayoutParams.BOTTOM_CENTER));
		} else if(ServiceTurnOver1.windowWidth == 540) {
			ServiceTurnOver1.mMapView.updateViewLayout(
					ServiceTurnOver1.mPopView, new MapView.LayoutParams(
							456, 130,
							pt, 0,
							0 - 66,				//ServiceTurnOver1.mPopView.getHeight()/3
							MapView.LayoutParams.BOTTOM_CENTER));
		} else {
			ServiceTurnOver1.mMapView.updateViewLayout(
					ServiceTurnOver1.mPopView, new MapView.LayoutParams(
							405, 115,
							pt, 0,
							0 - 61,				//ServiceTurnOver1.mPopView.getHeight()/3
							MapView.LayoutParams.BOTTOM_CENTER));
		}

		/*if(ServiceTurnOver1.application.locData != null && !title.equals("my") && ServiceTurnOver1.localCity) {
			float[] distance = new float[1];
			Location.distanceBetween(ServiceTurnOver1.application.locData.latitude, ServiceTurnOver1.application.locData.longitude, mGeoList.get(index).getPoint().getLatitudeE6()/1000000.00, mGeoList.get(index).getPoint().getLongitudeE6()/1000000.00, distance);
			int km = (int) (distance[0] / 1000);
			int minute = (km * 60) / ServiceTurnOver1.speed;
			if(km == 0) {
				ServiceTurnOver1.toSiteDistance.setText("<1KM");
				ServiceTurnOver1.need_time.setText("1分钟");
				ServiceTurnOver1.tip.setVisibility(View.VISIBLE);
				ServiceTurnOver1.tip.setBackgroundColor(Color.BLACK);
			} else {
				ServiceTurnOver1.need_time.setText(minute + "分钟");
				ServiceTurnOver1.tip.setVisibility(View.VISIBLE);
				ServiceTurnOver1.tip.setBackgroundColor(Color.BLACK);
				ServiceTurnOver1.toSiteDistance.setText(km + "KM");
			}
		}*/
		
		if(i == 0) {
			prePoint = mGeoList.get(index).getPoint();
		}
		if(prePoint.getLatitudeE6() == mGeoList.get(index).getPoint().getLatitudeE6() && prePoint.getLongitudeE6() == mGeoList.get(index).getPoint().getLongitudeE6()) {
			ServiceTurnOver1.mPopView.setVisibility(View.VISIBLE);
		} else {
			prePoint = mGeoList.get(index).getPoint();
		}
		ServiceTurnOver1.mMapView.getController().animateTo(
				mGeoList.get(index).getPoint());
		
		
//		 ServiceTurnOver.mPopView.setVisibility(View.VISIBLE);
		ServiceTurnOver1.animation = true;
		i++;
		
		for(int j = 0; j < mGeoList.size(); j++) {
			if(j == index) {
				mGeoList.get(j).setMarker(ServiceTurnOver1.resources.getDrawable(R.drawable.mark_blue));
			} else {
				mGeoList.get(j).setMarker(ServiceTurnOver1.resources.getDrawable(R.drawable.mark_red));
			}
			
			ServiceTurnOver1.tap = true;
		}
		ServiceTurnOver1.mMapView.refresh();
		return true;
	}

	@Override
	public boolean onTap(GeoPoint pt, MapView mapView) {
		if (ServiceTurnOver1.mPopView != null) {
			ServiceTurnOver1.mPopView.setVisibility(View.GONE);
			ServiceTurnOver1.tip.setBackgroundColor(Color.parseColor("#00000000"));
			ServiceTurnOver1.tip.setVisibility(View.INVISIBLE);
		}
		super.onTap(pt, mapView);
		return false;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mGeoList.get(i);
	}

	@Override
	public int size() {
		return mGeoList.size();
	}

	public void addItem(OverlayItem item) {
		mGeoList.add(item);
		populate();
	}

	public void removeItem(int index) {
		mGeoList.remove(index);
		populate();
	}
	
	public void removeAll() {
		mGeoList.clear();
	}
	
	public List<OverlayItem> getItem() {
		return mGeoList;
	}
	
} 

/*
private void applyRotation(int position, float start, float end) {
    // Find the center of the container
    final float centerX = mContainer.getWidth() / 2.0f;
    final float centerY = mContainer.getHeight() / 2.0f;

    // Create a new 3D rotation with the supplied parameter
    // The animation listener is used to trigger the next animation
    final Rotate3dAnimation rotation =
            new Rotate3dAnimation(start, end, centerX, centerY, 0, true);
    rotation.setDuration(500);
    rotation.setFillAfter(true);
    rotation.setInterpolator(new AccelerateInterpolator());
    rotation.setAnimationListener(new DisplayNextView(position));

    mContainer.startAnimation(rotation);
}

private final class DisplayNextView implements Animation.AnimationListener {
    private final int mPosition;

    private DisplayNextView(int position) {
        mPosition = position;
    }

    public void onAnimationStart(Animation animation) {
    }

    public void onAnimationEnd(Animation animation) {
        mContainer.post(new SwapViews(mPosition));
    }

    public void onAnimationRepeat(Animation animation) {
    }
}

*//**
 * This class is responsible for swapping the views and start the second
 * half of the animation.
 *//*
private final class SwapViews implements Runnable {
    private final int mPosition;

    public SwapViews(int position) {
        mPosition = position;
    }

    public void run() {
        final float centerX = mContainer.getWidth() / 2.0f;
        final float centerY = mContainer.getHeight() / 2.0f;
        Rotate3dAnimation rotation;
        
        if (mPosition > -1) {
        	map_modern.setVisibility(View.GONE);
        	list_modern.setVisibility(View.VISIBLE);
        	list_modern.requestFocus();

//            rotation = new Rotate3dAnimation(180, 360, centerX, centerY, 310.0f, false);
        	rotation = new Rotate3dAnimation(270, 360, centerX, centerY, 0f, false);
        } else {
        	list_modern.setVisibility(View.GONE);
        	map_modern.setVisibility(View.VISIBLE);
        	map_modern.requestFocus();

//            rotation = new Rotate3dAnimation(180, 0, centerX, centerY, 310.0f, false);
        	rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 0f, false);
        }

        rotation.setDuration(500);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new DecelerateInterpolator());

        mContainer.startAnimation(rotation);
        
        //默认是列表时，跳转到地图模式，动画结束后发送消息
        if(!sendMessage) {
        	showProgress("正在加载数据...");
			mHandler_map.sendEmptyMessage(ProApplication.location_cuccess);
			sendMessage = true;
		}
    }
}*/